package cn.xclink.kernel.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xclink.kernel.entity.Org;
import cn.xclink.kernel.service.OrgService;
import cn.xclink.kernel.service.RoleService;
import cn.xclink.kernel.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/common/lookup")
public class LookupController {

    public static String USER = "user";
    public static String ORG = "org";
    public static String ROLE = "role";

    @Autowired
    private UserService userService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/{lookupType}", method = RequestMethod.GET)
    public String index(@PathVariable("lookupType") String lookupType,
                        Model model) {
        return "common/lookup/" + lookupType;
    }

    @RequestMapping(value = "/list/{lookupType}", method = RequestMethod.POST)
    @ResponseBody
    public Object getList(@PathVariable("lookupType") String lookupType) {

        Object object = new Object();
        if (USER.equals(lookupType)) {
            object = userService.findAll();
        }
        if (ORG.equals(lookupType)) {

            List<Org> list = orgService.findAll();
            for (Org item : list) {
                item.setRender("{orgId:'" + item.getOrgId() + "',orgName:'"
                        + item.getOrgName() + "',orgCode:'" + item.getOrgCode()
                        + "'}");
            }
            return list;
        }
        if (ROLE.equals(lookupType)) {
            object = roleService.findAll();
        }
        return object;
    }

    @RequestMapping(value = "/map/{lookupType}", method = RequestMethod.POST)
    @ResponseBody
    public Object getMap(@PathVariable("lookupType") String lookupType) {

        Map<Object, Object> map = new HashMap<Object, Object>();
        if (USER.equals(lookupType)) {

        }
        if (ORG.equals(lookupType)) {

            List<Org> list = orgService.findAll();
            for (Org item : list) {
                item.setRender("{orgId:'" + item.getOrgId() + "',orgName:'"
                        + item.getOrgName() + "',orgCode:'" + item.getOrgCode()
                        + "'}");
            }
        }
        if (ROLE.equals(lookupType)) {

        }
        return map;
    }
}
